package org.adit.spring.hibernate.controller;

import java.util.List;

import org.adit.spring.hibernate.component.RouteSession;
import org.adit.spring.hibernate.dao.RecursoDao;
import org.adit.spring.hibernate.dao.ResourceTypeDao;
import org.adit.spring.hibernate.dao.RouteDao;
import org.adit.spring.hibernate.entity.Recurso;
import org.adit.spring.hibernate.entity.ResourceType;
import org.adit.spring.hibernate.entity.Route;
import org.adit.spring.hibernate.infra.Restrito;

import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Put;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.view.Results;

@Resource
public class RecursosController {

	private RecursoDao dao;
	private RouteDao routeDao;
	private ResourceTypeDao resourceTypeDao;

	private RouteSession routeSession;
	private final Result result;

	public RecursosController(ResourceTypeDao resourceTypeDao, RouteSession routeSession, RecursoDao dao, Result result, RouteDao routeDao) {
		this.routeDao = routeDao;
		this.resourceTypeDao = resourceTypeDao;
		this.routeSession = routeSession;
		this.dao = dao;
		this.result = result;
	}

	@Restrito
	@Post
	@Path("/recursos")
	public void adiciona(Recurso recurso) {
		Route route = new Route();
		route = routeDao.carrega(recurso.getRoute().getId());
		recurso.setRoute(route);
		routeSession.setRoute(route);
		dao.salva(recurso);
		result.redirectTo(RecursosController.class).lista(recurso.getRoute().getId());
	}

	@Restrito
	@Get
	@Path("/recursos/novo/{id}")
	public void formulario(Long id) {
		Route route = new Route();
		route.setId(id);
		List<ResourceType> resourceTypes = resourceTypeDao.listaTudo();
		route.setResourceTypes(resourceTypes);
		result.include("route",route);
	}

	@Restrito
	@Get
	@Path("/recursos/{id}")
	public Recurso edita(Long id) {
		return dao.carrega(id);
	}

	@Restrito
	@Put
	@Path("/recursos/{recurso.id}")
	public void altera(Recurso recurso) {
		dao.atualiza(recurso);
		result.redirectTo(RecursosController.class).lista(recurso.getRoute().getId());
	}

	@Restrito
	@Delete
	@Path("/recursos/{id}")
	public void remove(Long id) {
		Recurso recurso = dao.carrega(id);
		Route route = new Route();
		route.setId(recurso.getId());
		dao.remove(recurso);
		result.redirectTo(RecursosController.class).lista(route.getId());
	}

	/**
	 * asdjaslkdsj
	 * 
	 * asdas
	 * @param id
	 * @return List<Recurso>
	 */
	@Get
	@Path("/recursos")
	public List<Recurso> lista(Long id) {
		if (id != null) {
			Route route = new Route();
			route = routeDao.carrega(id);
			routeSession.setRoute(route);
		}
		return dao.listaTudo();
	}

	@Get
	@Path("/recursos/busca")
	public List<Recurso> busca() {
		return dao.listaTudo();
	}

	public List<Recurso> busca(String nome) {
		result.include("nome", nome);
		return dao.busca(nome);
	}

	@Get
	@Path("/recursos/busca.json")
	public void buscaJson(String q) {
		result.use(Results.json()).from(dao.busca(q)).exclude("id", "descricao").serialize();
	}
}